import { Component, ComponentRef, Injector, OnDestroy, OnInit, ViewChild, ViewContainerRef } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import {
    ContentConfig, PagedContentInput
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/api';
import { contentListComponent } from './content-list.component';
import { IContentComponent, IGoRelatePageDto } from '../IContentComponent';
import { IContentDetailComponent } from '../IContentDetailComponent';
import { ContentDetailModalComponent } from '../detail/content-detail-modal.component';
import { AppContentService } from '../app-content.Service';
import { AppContentDetailService } from '../app-content-detail.Service';
import { ContentModalComponent } from './content-modal.component';
import { SeoModalComponent } from '@app/main/shared/seo/seo-modal.component';
import { OrderByModalComponent } from '@app/main/shared/order-by/order-by-modal.component';
import { Content2ModalComponent } from '@app/main/content2/list/content2-modal.component';

@Component({
    templateUrl: './content.component.html',
    animations: [appModuleAnimation()]
})
export class ContentComponent extends AppComponentBase implements IContentComponent, IContentDetailComponent, OnInit, OnDestroy {
    
    @ViewChild('contentListTab') contentListTab: contentListComponent;
    @ViewChild('contentDetailModal', { static: true }) contentDetailModal: ContentDetailModalComponent;
    @ViewChild('contentListModal', { static: true }) contentListModal: ContentModalComponent;
    @ViewChild('content2ListModal', { static: true }) content2ListModal: Content2ModalComponent;
    
    @ViewChild('seoModal', { static: true }) seoModal: SeoModalComponent;

    @ViewChild('orderByModal', { static:  true }) orderByModal: OrderByModalComponent;  

    @ViewChild('vc', { read: ViewContainerRef }) vc: ViewContainerRef;

    cmpRef: ComponentRef<any>;

    isLoading: boolean =  false;

    kind: number;
    customKey: string;
    relateId: string;

    inputPara: PagedContentInput;

    permissionDic: { [key: string]: boolean } = {};

    pageConfig: ContentConfig;

    hiddenList: boolean = true;
    hiddenDetail: boolean = true;

    isModal: boolean = false;

    advancedFiltersAreShown: boolean = false;

    dataSynchronous: boolean = false;

    listDataCacheEnable: boolean = false;

    //private navigationSubscription;

    constructor(
        injector: Injector,
        private router: Router,
        private _activatedRoute: ActivatedRoute,
        private _contentService: AppContentService,
        private _contentDetailService: AppContentDetailService
    ) {
        super(injector);
        
        this.inputPara = new PagedContentInput();
        
        this.dataSynchronous =  this.setting.getBoolean("App.Synchronous.DataSynchronous");
        this.listDataCacheEnable =  this.setting.getBoolean("App.Cache.ListResult");
    }

    ngOnInit() {
        
        this._activatedRoute.params.subscribe((params: Params) => {
            //console.log(params);
            if (this.kind > 0) {
                this.resetParamDatas();
            }
            this.kind = Number(params['kind']);

            this._contentService.initListPage(this, null);
        });

        //// 这个订阅不主动取消的话会一直在，页面销毁后也一直在
        //this.navigationSubscription = this.router.events.subscribe((event: any) => {
        //    if (event instanceof NavigationEnd) {
        //        this.resetParamDatas();
        //        console.log(event);
        //        this._contentService.initListPage(this, null);
        //    }
        //});
    }

    private resetParamDatas() {
        this.inputPara.filter = "";
        this.inputPara.infoState = "#";
        this.permissionDic = {};

        this.advancedFiltersAreShown = false;

        this.hiddenList = true;
        this.hiddenDetail = true;

        if (this.cmpRef) {
            this.cmpRef.destroy();
        }
    }

    getListData(event?: LazyLoadEvent) {
        if (event) {
            this._contentService.getList(this, event);
        } else {
            this._contentService.getList(this);
        }
    }

    goRelatePage(data: IGoRelatePageDto): void {
        if (data.relateDataContentType == "content2") {
            this.content2ListModal.show(data.id, this.kind, data.relateDataCustomKey, data.displayName);
        } else {
            this.contentListModal.show(data.id, this.kind, data.relateDataCustomKey, data.displayName);
        }
    }

    delete() {
        this._contentService.deleteItems(this);
    }

    changeState(state: boolean) {
        this._contentService.changeState(this, state);
    }

    reloadPage() {
        this._contentService.reloadPage(this);
    }

    // 内容单页自动切换为详细页
    gotoDetail(id: string) {
        this.id = id;

        this._contentDetailService.initDetailPage(this, this.vc.injector,false, null);
    }

    editModal(record: { [key: string]: any; }) {
        this._contentService.openCreateOrEdit(this, record.id);
    }


    create(): void {
        this._contentService.openCreateOrEdit(this, null);
    }

    swapOrder(){
        this._contentService.swapOrder(this);
    }
    
    synchorization(): void {
        this._contentService.synchorization(this);
    }

    resetListApiCache() {
        this._contentService.resetListApiCache();
    }

    //------内容单页------------------
    id: string = '';
    randomId: string = '';
    isTemporary: boolean = false;
    saving: boolean = false;
    isActive: boolean = false;

    //权限校验
    ctrlDisabled = false;

    data: { [key: string]: any } = {};

    // id无效等 未找到数据 自动为新建
    goToNewPage() {
        this.id = null;

        this._contentDetailService.initDetailPage(this, this.vc.injector, false, null);
    }
    refreshData(){
        this._contentDetailService.initDetailPage(this, this.vc.injector, false, null);
    }

    ngOnDestroy() {
        if (this.cmpRef) {
            this.cmpRef.destroy();
        }
        //if (this.navigationSubscription) {
        //    this.navigationSubscription.unsubscribe();
        //}
    }

    synchorizationById(): void {
        this._contentDetailService.synchorizationById(this);
    }
    synchorization2(): void {
        this._contentDetailService.synchorization(this);
    }

    changeSingleState(state: boolean) {
        this._contentDetailService.changeState(this, state);
    }

    deleteSingle() {
        this._contentDetailService.deleteSingle(this);
    }

    goSeoModal(data?: any) {
        if (data) {
            this.seoModal.show(data.id, this.kind, data.title);
        } else {
            this.seoModal.show(this.id, this.kind, this.data.title);
        }
    }

    save() {
        this._contentDetailService.saveItem(this);
    }
}
